package com.appcheck.recording.handler.impl;

import android.view.View;
import android.widget.EditText;

import com.appcheck.recording.ListenerManager;
import com.appcheck.recording.WidgetHelper;
import com.appcheck.recording.action.impl.FocusAction;
import com.appcheck.recording.action.impl.KeyAction;
import com.appcheck.recording.action.impl.TextChangedAction;
import com.appcheck.recording.action.impl.TouchAction;
import com.appcheck.recording.handler.action.TextEnteredAction;
import com.appcheck.recording.plugin.ActionConverter;

public final class EditTextGrouper extends Grouper {
	private String content;
	private EditText editText;
	
	public EditTextGrouper(Grouper paramGrouper){
		super(paramGrouper);
	}
	
	private boolean checkContentNotNull(){
		if((this.content != null) && (!this.content.equals("")))
			return true;
		else return false;
	}
	
	public final boolean visit(TouchAction action){
		//Logger.INFO(action.toString());
		if(action.getActionCode() == android.view.MotionEvent.ACTION_DOWN){
			if((action.getView() instanceof EditText)){
				//如果当前点的是EditText
				if((this.editText != null) && (this.editText != action.getView()) && checkContentNotNull()) {
					//如果当前点的EditText和之前保存的EditText不一样，且之前保存了输入内容，则结束输入
					endEnterAction();
				}
				//保存当前点的EditText
				this.editText = ((EditText)action.getView());
			}else if(WidgetHelper.check(action.getView())){
				//点的不是EditText检查那两个参数是否和view的id相同
				this.editText = null;
				this.content = null;
			}else if(checkContentNotNull() && (this.editText != null)){
				//点的不是EditText，参数不同且之前保存的内容不为空
				endEnterAction();
			}
		}
		return false;
	}
	
	public final boolean visit(TextChangedAction action){
	    this.content = action.getText();
	    if (this.editText == null) {
	    	View focusedView = ListenerManager.getListenerManager().getFocusedView();
	    	if ((focusedView != null) && ((focusedView instanceof EditText))) {
	    		this.editText = ((EditText)focusedView);
	    	}
	    }
	    return true;
	}
	
	public final boolean visit(FocusAction action){
	    if ((action.getView() instanceof EditText)) {
	        this.editText = ((EditText)action.getView());
	      }
	    return true;
	}
	
	public final boolean visit(KeyAction action){
	    if ((action.getKeyCode() == android.view.KeyEvent.KEYCODE_ENTER) && (this.editText != null)){
	    	//检查EditText的InputType，这里的值即是layout中xml中指定的android:inputType的值
	    	if ((this.editText.getInputType() & android.text.InputType.TYPE_TEXT_FLAG_MULTI_LINE) ==
	    			android.text.InputType.TYPE_TEXT_FLAG_MULTI_LINE) {
	    		//如果是多行的EditText而且没指定其他的格式
	    		return true;
	    	}
	    	if (checkContentNotNull()){
	    		//content不为空，包含输入内容
	    		endEnterAction();
	    		return false;
	    	}
	    }
	    return false;
	}
	
	private void endEnterAction(){
		
	    TextEnteredAction textEnteredAction = new TextEnteredAction();
	    textEnteredAction.setContent(this.content);
	    textEnteredAction.setResourceName(WidgetHelper.getResourceName(this.editText));
	    if (textEnteredAction.getResourceName() == null) {
	    	textEnteredAction.setIndex(WidgetHelper.getCurrentIndexOfView(this.editText));
	    }
	    forwardActionToPlugin(textEnteredAction);
	    this.content = null;
	    this.editText = null;
	}
}
